package com.niiwoo.civet.trade.service.local.loan.creditaduit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niiwoo.civet.trade.dao.entity.ProjectFlowExecute;
import com.niiwoo.civet.trade.dao.mapper.ProjectFlowExecuteMapperExt;
import com.niiwoo.civet.trade.enums.ProjectTypeEnum;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CreditAuditCompositeService {
	@Autowired
	private CreditCardLoanCreditAduitService creditCardLoanCreditAduitService;
	@Autowired
	private SmallLoanCreditAduitService smallLoanCreditAduitService;
	@Autowired
	private SocailSecurityLoanCreditAduitService socailSecurityLoanCreditAduitService;
	@Autowired
	private ConsumeLoanCreditAduitService consumeLoanCreditAduitService;
	@Autowired
	private ProjectFlowExecuteMapperExt projectFlowExecuteMapperExt;

	/**
	 * 调用天秤信审接口
	 * 
	 * @param userId
	 * @param projectId
	 * @param flowId
	 */
	public void callTianchengCreditAuditService(String userId, Long projectId, Long flowExecuteId, Byte projecType) {
		ProjectTypeEnum projecTypeEnum = ProjectTypeEnum.getEnum(projecType);
		switch (projecTypeEnum) {
    	case CREDIT_CARD_LOAN: //信用卡贷
    		creditCardLoanCreditAduitService.toRequestAutoAuditModel(userId, projectId, flowExecuteId);
    		break;
    	case SMALL_LOAN: //小易贷
    		smallLoanCreditAduitService.toRequestAutoAuditModel(userId, projectId, flowExecuteId);
    		break;
    	case SOCIAL_SECURITY_LOAN: //社保贷
    		socailSecurityLoanCreditAduitService.toRequestAutoAuditModel(userId, projectId, flowExecuteId);
    		break;
    	case CONSUME_LOAN: //消费贷
    		consumeLoanCreditAduitService.toRequestAutoAuditModel(userId, projectId, flowExecuteId);
    		break;    		
		default:
			log.error("借款流程数据异常,产品类型错误={},流程ID={}", projecType, flowExecuteId);
		}
	}
	
	public void callTianchengCreditAuditService(Long flowExecuteId) {
		ProjectFlowExecute projectFlowExecute = projectFlowExecuteMapperExt.selectByPrimaryKey(flowExecuteId);
		if (projectFlowExecute == null) {
			log.error("借款流程数据异常,流程不存在,流程ID={}", flowExecuteId);
			return;
		}

		ProjectTypeEnum projectTypeEnum = ProjectTypeEnum.getEnum(projectFlowExecute.getProjectType());
		if (projectTypeEnum == null) {
			log.error("借款流程数据异常,产品类型不存在,流程ID={}", flowExecuteId);
			return;
		}

		callTianchengCreditAuditService(projectFlowExecute.getBorrowUserId(), projectFlowExecute.getProjectId(),
				projectFlowExecute.getFlowExecuteId(), projectFlowExecute.getProjectType());
	}

	/**
	 * 调用天秤补漏接口
	 * 
	 * @param userId
	 * @param projectId
	 * @param flowId
	 * @param projecType
	 */
	public void callTianchengRepairService(String userId, Long projectId, Long flowId, Byte projecType) {
		ProjectTypeEnum projecTypeEnum = ProjectTypeEnum.getEnum(projecType);
		switch (projecTypeEnum) {
		case SMALL_LOAN: //小易贷
			smallLoanCreditAduitService.toGetTianchengCreditAuditResult(userId, projectId, flowId);
			break;
		case SOCIAL_SECURITY_LOAN: //社保贷
			socailSecurityLoanCreditAduitService.toGetTianchengCreditAuditResult(userId, projectId, flowId);
			break;
		case CREDIT_CARD_LOAN: //信用卡贷
			creditCardLoanCreditAduitService.toGetTianchengCreditAuditResult(userId, projectId, flowId);
			break;
		case CONSUME_LOAN: //消费贷
			consumeLoanCreditAduitService.toGetTianchengCreditAuditResult(userId, projectId, flowId);
			break;			
		default:
			return;
		}
	}

	public void callTianchengRepairService(Long flowExecuteId) {
		ProjectFlowExecute projectFlowExecute = projectFlowExecuteMapperExt.selectByPrimaryKey(flowExecuteId);
		if (projectFlowExecute == null) {
			log.error("借款流程数据异常,流程不存在,流程ID={}", flowExecuteId);
			return;
		}

		ProjectTypeEnum projectTypeEnum = ProjectTypeEnum.getEnum(projectFlowExecute.getProjectType());
		if (projectTypeEnum == null) {
			log.error("借款流程数据异常,产品类型不存在,流程ID={}", flowExecuteId);
			return;
		}
		
		callTianchengRepairService(projectFlowExecute.getBorrowUserId(), projectFlowExecute.getProjectId(),
				projectFlowExecute.getFlowExecuteId(), projectFlowExecute.getProjectType());
	}

}
